//
//  DDMessageSettingViewController.m
//  DingDongZP
//
//  Created by zurich on 16/3/29.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDMessageSettingViewController.h"
#import "DDChatSettingCell.h"
#import <RongIMLib/RongIMLib.h>

@interface DDMessageSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)  UITableView *settingTableView;
@property (strong ,nonatomic) NSArray   *settingTitleArray;
@end

@implementation DDMessageSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.settingTitleArray = @[@"聊天置顶",@"将对方加入黑名单"];
    self.settingTableView = [[UITableView alloc] init];
    
    [self.settingTableView setDelegate:self];
    [self.settingTableView setDataSource:self];
    [self.view addSubview:_settingTableView];
    
    self.settingTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.settingTableView.rowHeight = 54.f;
    [self.settingTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    if ([self.settingTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.settingTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.settingTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.settingTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}


- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"settingCell";
    
    DDChatSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = (DDChatSettingCell *)[[[NSBundle mainBundle] loadNibNamed:@"DDChatSettingCell" owner:self options:nil] lastObject];
    }
    cell.titleLabel.text = self.settingTitleArray[indexPath.row];
    cell.titleLabel.textColor = COLOR_FONT_DARK_GRAY;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //  置顶
    if (indexPath.row == 0) {
        
        RCConversation *conver = [[RCIMClient sharedRCIMClient] getConversation:self.conversationType targetId:self.targetId];
        cell.setSwitch.on = conver.isTop;
        
        cell.onSwitchChanged = ^(UISwitch *sw) {
            
            [[RCIMClient sharedRCIMClient] setConversationToTop:self.conversationType targetId:self.targetId isTop:sw.isOn];
            return YES;
        };
    }
    
    //  黑名单
    if (indexPath.row == 1) {
        
        [[RCIMClient sharedRCIMClient] getBlacklistStatus:self.targetId success:^(int bizStatus) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cell.setSwitch.on = bizStatus == 0;
            });
        } error:^(RCErrorCode status) {
            
        }];
        
        cell.onSwitchChanged = ^(UISwitch *sw) {
            
            if (sw.on) {
                [[RCIMClient sharedRCIMClient] addToBlacklist:self.targetId success:^{
                    
                } error:^(RCErrorCode status) {
                }];
            } else {
                [[RCIMClient sharedRCIMClient] removeFromBlacklist:self.targetId success:nil error:nil];
            }
            
            return YES;
        };
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
